#!/bin/bash
##
##第一个参数：运行环境，pro或test
##第二个参数：生成zip包地址
##第三个参数：是否执行mvn test，true或false
##
CUR_DIR=$(cd "$(dirname "${BASH_SOURCE-$0}")"; pwd)

BASE_PATH=${CUR_DIR}

AERO_DATA_PLATFORM_SRC_PATH="${BASE_PATH}/../../aero-data-platform"
TRIP_ANALYSIS_COMMON_SRC_PATH="${BASE_PATH}/../../opentsp-location-tripanalysis/opentsp-location-tripanalysis-common"
TRIP_ANALYSIS_APP_SRC_PATH="${BASE_PATH}/../../opentsp-location-tripanalysis/opentsp-location-tripanalysis-offline"

folderName=data_platform_job
export theme=$1

if [ $# -ge 1 ]; then
    echo "Shell 传递参数$#个！"
	echo "执行的文件名：$0"
	echo "第一个参数为：$1"
	echo "第二个参数为：$2"
	echo "第三个参数为：$3"
else
    echo "参数错误！"
    echo "执行的文件名：$0 第一个参数 "
	echo "第一个参数【theme】"
    exit 1
fi

if [ -n "$2" ];then
    OUT_PATH=$2
else
   OUT_PATH=/Users/shen/tmp
fi


#如果第三个参数为false，则不进行maven clean，直接生成布署包
if [ "false" == "$3" ];then
  mvn_package="&& mvn  package  -Dmaven.test.skip=true"
  mvn_install="&& mvn install -Dmaven.test.skip=true"
else
  mvn_package="&& mvn clean package"
  mvn_install="&& mvn clean install"
fi


#初始化输出文件夹
OUT_PATH=${OUT_PATH}/${theme}
if [ -d "${OUT_PATH}" ]
then
	rm -rf ${OUT_PATH}
fi
mkdir -p ${OUT_PATH}

#打包spark程序到指定文件夹
mvnPackageApp(){
    srcBasePath=$1
    appName=$2
    str1="cd ${srcBasePath} ${mvn_package} && unzip target/$(ls -alh ${srcBasePath}/target/ |grep zip |grep ${appName} |awk '{print $9}') -d ${OUT_PATH}/${folderName}/"
    echo $str1
    bash -c "$str1"
    #改名
    mv ${OUT_PATH}/${folderName}/$(ls -alh ${OUT_PATH}/${folderName}/ |grep ${appName} |awk '{print $9}') ${OUT_PATH}/${folderName}/${appName}
    #bin目录加上可执行权限
    chmod -R 755 ${OUT_PATH}/${folderName}/${appName}/bin
}
#依赖库打包
mvnInstallDep(){
    srcBasePath=$1
    str1="cd ${srcBasePath} ${mvn_install} "
    echo $str1
    bash -c "$str1"
}


echo "swtichEnv:${theme}"
echo "OUT_PATH:${OUT_PATH}"

#将代码复制到输出文件夹
cp -rf "${BASE_PATH}/${folderName}" ${OUT_PATH}/
# 驾驶行为平台
#mvnInstallDep "${TRIP_ANALYSIS_COMMON_SRC_PATH}"
#mvnPackageApp "${TRIP_ANALYSIS_APP_SRC_PATH}" "tripanalysis-offline"

# 统计分析平台
#mvnPackageApp "${AERO_DATA_PLATFORM_SRC_PATH}" "aero-data-platform"

#替换配置
#cp -rf ${BASE_PATH}/config_env/${folderName}/${theme}/* "${OUT_PATH}/${folderName}/"

#打包成zip格式
bash -c  "cd ${OUT_PATH} && zip -r ${OUT_PATH}/${folderName}_${theme}.zip ${folderName}/*"

#输出打包后的文件路径
echo "${OUT_PATH}/${folderName}_${theme}.zip"
ls -alh ${OUT_PATH}/

